var controllers = {};



controllers.BlogsController = function ($scope, $http, $log) {	

	$scope.test = function() {

		$http.defaults.headers.common.Authorization = 'OAuth oeAA9bDFbPfOFGJ3mbANac2jzxc'

		$http({method: 'GET', url: 'https://mq-aws-us-east-1.iron.io/1/projects/533cd079fc8aad0005000055/queues/my_queue/messages'}).
		    success(function(data, status, headers, config) {
		    	if(data.messages.length > 0)
		    		console.log(data.messages[0].body);
		    	else
		    		console.log('het roi anh');
		    }).
		    error(function(data, status, headers, config) {
		    	alert('error');
		    });
	};


	$scope.test1 = function() {

		$http.defaults.headers.common.Authorization = 'OAuth oeAA9bDFbPfOFGJ3mbANac2jzxc'

		this.data = {
			messages:[{body:"dai ka an kut"}]
		};
		
		this.url = 'https://mq-aws-us-east-1.iron.io/1/projects/533cd079fc8aad0005000055/queues/my_queue/messages',
		

		$http.post(this.url, this.data).
		    success(function(data, status, headers, config) {
		    	console.log(data);
		    }).
		    error(function(data, status, headers, config) {
		    	alert('error');
		    });
	};
	
	
	
	$http({method: 'GET', url: 'http://localhost:9900/api/topics'}).
	
    success(function(data, status, headers, config) {	
    	$scope.topics = data.response.topics;    	
    }).
    error(function(data, status, headers, config) {
    	alert('error');
    });
	
	
	
	
//	console.log(config.apiUrl + config.resources.blogTopic);
	
	$scope.addTopic = function() {
		
		
//		this.data = {
//				messages:[{body:"dai ka an kut"}]
//			};
//		
		this.url = config.apiUrl + config.resources.blogTopic;
		this.data = {
				blogId: "blog.com", 
				topics: "Id1,Id2",
		};
//		$http.post(this.url, this.data);		
//		
//		alert('aaaa');
		$http.post(this.url, this.data).
		    success(function(data, status, headers, config) {
		    	console.log(data);
		    }).
		    error(function(data, status, headers, config) {
		    	console.log("eeeee" + data);
		      // called asynchronously if an error occurs
		      // or server returns response with an error status.
		    });
	};
	
	
	
	
	
	// alert
	
	$scope.alerts = [
		{ type: 'danger', msg: 'Oh snap! Change a few things up and try submitting again.' },
		{ type: 'success', msg: 'Well done! You successfully read this important alert message.' }
	];
	$scope.closeAlert = function(index) {
	    $scope.alerts.splice(index, 1);
	};


	$scope.getBlogInfo = function(){
		$scope.url = config.apiUrl + config.resources.blogs + "/" + $('#hfr-blogName').val();
		$http.get($scope.url).
		success(function(data, status, headers, config) {
			$scope.blogAuthKey = data.response.blog.authKey;
			$scope.blogBloggerId = data.response.blog.bloggerId;
			$scope.blogCustomerId = data.response.blog.customerId;
			$scope.blogNumberPost = data.response.blog.numberPost;
			$scope.blogPassword = data.response.blog.password;
			$scope.blogScore = data.response.blog.score;
			$scope.blogTokenType = data.response.blog.tokenType;
			$scope.blogType = data.response.blog.type;
			$scope.blogUrl = data.response.blog.url;
			$scope.blogUsername = data.response.blog.username;

	    	console.log(data);
	    }).
	    error(function(data, status, headers, config) {

	    	console.log("eeeee" + data);
	    });
	};


};

hfrApp.controller(controllers);